package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.dao.AddTo;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 刘琪 on 15/11/6.
 */
public class CSongListAdapter extends BaseAdapter {
    private List<AddTo> addTos;
    private Context context;
    private AddTo addTo;

    public CSongListAdapter(Context context, List<AddTo> addTos) {
        this.context = context;
        this.addTos = addTos;
    }

    public void longClick(AddTo addTo){
        addTos.remove(addTo);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return addTos != null && addTos.size() > 0 ? addTos.size() : 0;
    }

    @Override
    public AddTo getItem(int position) {
        return addTos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.collection_songlist_item, null);
            holder.collection_listview_item_imgs = (ImageView) convertView.findViewById(R.id.collection_listview_item_imgs);
            holder.collection_listview_item_music_name = (TextView) convertView.findViewById(R.id.collection_listview_item_music_name);
            holder.collection_listview_item_music_count = (TextView) convertView.findViewById(R.id.collection_listview_item_music_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        addTo = getItem(position);

        if (addTo != null) {
            // 专辑图片
            Picasso.with(context).load(addTo.getPicUrl()).placeholder(R.mipmap.moving)
                    .error(R.mipmap.moving).into(holder.collection_listview_item_imgs);
            // 歌单名
            holder.collection_listview_item_music_name.setText(addTo.getName());
            // 歌曲数量
            holder.collection_listview_item_music_count
                    .setText(addTo.getSongs() + "首");
        }

        return convertView;
    }

    private class ViewHolder {
        private ImageView collection_listview_item_imgs;
        private TextView collection_listview_item_music_name;
        private TextView collection_listview_item_music_count;
    }
}
